// pages/product/product.js
Page({
  pageNo:1,
  flag:false,
  data: {
    products:[]
  },

  onLoad: function (options) {
    let self=this
    wx.request({
      url: `http://localhost:3000/api/getproducts?pageSize=8&pageNo=${this.pageNo}`,
      dataType: 'json',
      success: function(res) {
        self.setData({
          products: res.data.products
        })
      },
    })
  },

  onReachBottom: function () {
    let self = this
    if(self.flag){
       return
    }
    this.flag=true
    this.pageNo++
    wx.request({
      url: `http://localhost:3000/api/getproducts?pageSize=8&pageNo=${this.pageNo}`,
      dataType: 'json',
      success: function (res) {
        // console.log(res, self.data.products)
        self.setData({
          products: [...self.data.products,...res.data.products]
        })
        self.flag = false
        // console.log(self.data.products)
      },
    })
  },
  getProduct(e){
  //  console.log(e)
    let pid = e.currentTarget.dataset.pid
    wx.navigateTo({
      url: '../productDetail/productDetail?pid='+pid,
    })
  }
})